﻿// SKNotes - a note taking and gathering tool
// Copyright (C) 2009  Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace SKNotes.CustomControls.HtmlEditor.Control
{
    using IfacesEnumsStructsClasses;

    public static class HtmlPopup
    {
        private static IHTMLPopup currentPopup;

        /// <summary>
        /// Shows the popup.
        /// </summary>
        /// <Remarks>
        /// Background color is set to Aqua and the border is set to 1px solid skyblue. Font is tahoma
        /// with a size of 12. A style filter is used to provided the gradient color in the background.
        /// Starting with skyblue with an end color of white.
        /// </Remarks>
        /// <param name="domDocument">The DOM document.</param>
        /// <param name="displayHtml">The display HTML.</param>
        /// <param name="location">The location.</param>
        public static void ShowPopup(object domDocument, string displayHtml, int x, int y, int width, int height)
        {
            IHTMLDocument2 doc2 = domDocument as IHTMLDocument2;

            if (doc2 != null)
            {
                IHTMLWindow4 window4 = doc2.parentWindow as IHTMLWindow4;

                if (window4 != null)
                {
                    object nullObject = null;
                    IHTMLPopup popup = (IfacesEnumsStructsClasses.IHTMLPopup)window4.createPopup(nullObject);
                    if (popup != null)
                    {
                        IHTMLDocument2 popupdoc = popup.document as IHTMLDocument2;
                        if (popupdoc != null && popupdoc.body != null)
                        {
                            // TODO: better colors for the tooltip
                            popupdoc.body.style.backgroundColor = "Aqua";
                            popupdoc.body.style.border = "1px solid black";
                            popupdoc.body.style.fontFamily = "tahoma";
                            popupdoc.body.style.fontSize = "12";
                            popupdoc.body.style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='skyblue', EndColorStr='white')";
                            popupdoc.body.innerHTML = displayHtml;
                        }

                        //x-coordinate, y-coordinate, width, height, element
                        popup.show(x + 10, y + 20, width, height, doc2.body);
                        currentPopup = popup;
                    }
                }
            }
        }

        /// <summary>
        /// Hides the popup.
        /// </summary>
        public static void HidePopup()
        {
            if (currentPopup != null)
            {
                currentPopup.hide();
            }
        }
    }
}
